const downloadExcel = (buffer, name = 'download') => {
  const blob = new Blob([buffer], { type: 'application/vnd.ms-excel' }) // application/vnd.openxmlformats-officedocument.spreadsheetml.sheet这里表示xlsx类型
  var downloadElement = document.createElement('a')
  var href = window.URL.createObjectURL(blob)
  downloadElement.href = href
  downloadElement.download = name
  document.body.appendChild(downloadElement)
  downloadElement.click()
  document.body.removeChild(downloadElement)
  window.URL.revokeObjectURL(href)
}

const binary2Base64 = binary => 'data:image/png;base64,' + btoa(
  new Uint8Array(binary).reduce((data, byte) => data + String.fromCharCode(byte), '')
)

const downloadFileByBuffer = (buffer, name = 'download') => {
  const blob = new Blob([buffer])
  if (window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(blob, name)
  } else {
    const blobUrl = window.URL.createObjectURL(blob)
    const a = document.createElement('a')
    a.href = blobUrl
    a.setAttribute('download', name)
    a.click()
  }
}

export {
  downloadExcel,
  binary2Base64,
  downloadFileByBuffer
}

